var t = require("../../../@babel/runtime/helpers/defineProperty"), e = getApp();

Page({
    data: {
        start: "",
        end: "",
        total: 1,
        order_info: {},
        start_time: "",
        end_time: "",
        night_day: "",
        name: "",
        mobil: "",
        isInvoice: !1,
        invoice_type: "1",
        el_invoice: "1",
        fp_tt: "",
        company_name: "",
        sbxd: "",
        title: "",
        ORDER_URL: "plugin.hotel.frontend.hotel.goods-buy",
        SUBMIT_URL: "plugin.hotel.frontend.hotel.create",
        id: "",
        language: "",
        isShowCoupon: !0,
        popupCouponSpecs: !1,
        checkCouponList: [],
        coupon_size: 0,
        use_coupon_size: 0,
        coupons: [],
        cup_notice: !0,
        checkDeductionList: [],
        hasGoodOpenDF: !1,
        orders_goods: [],
        diyShow: !1,
        diyform: {
            form_id: "",
            diyform_data_id: "",
            pre_id: ""
        }
    },
    onLoad: function(t) {
        t.end && this.setData({
            end: t.end
        }), t.start && this.setData({
            start: t.start
        }), t.id && this.setData({
            id: t.id
        }), t.goods_id && this.setData({
            goods_id: t.goods_id
        }), this.getOrerData(), this.getCustomizeHotelHead();
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    showDiy: function(t) {
        if (this._findDiyForm(t.currentTarget.dataset.preid)) {
            var e = this._findDiyForm(t.currentTarget.dataset.preid);
            this.setData({
                "diyform.diyform_data_id": e
            });
        }
        this.setData({
            diyShow: !0
        });
    },
    showDiyClose: function() {
        this.setData({
            diyShow: !1
        });
    },
    _initDiyForm: function() {
        for (var t = {}, e = 0; e < this.data.order_info.orders.length; e++) for (var i = 0; i < this.data.order_info.orders[e].order_goods.length; i++) if (this.data.order_info.orders[e].order_goods[i].diy_form) {
            var o = {};
            o.pre_id = this.data.order_info.orders[e].order_goods[i].pre_id, o.diyform_data_id = "", 
            t[o.pre_id] = o;
        }
        this.setData({
            orders_goods: t
        });
    },
    findGoodOpen: function() {
        for (var t = 0; t < this.data.order_info.orders.length; t++) for (var e = 0; e < this.data.order_info.orders[t].order_goods.length; e++) this.data.order_info.orders[t].order_goods[e].diy_form && (this.setData({
            "diyform.form_id": this.data.order_info.orders[t].order_goods[e].diy_form.form_id,
            "diyform.pre_id": this.data.order_info.orders[t].order_goods[e].pre_id,
            hasGoodOpenDF: !0
        }), console.log("有商品开启自定义表单！！！"));
    },
    traCheckedData: function(t) {
        this._setDiyForm(t.detail.pre_id, t.detail.diyform_data_id), this.showDiyClose();
    },
    _checkDiyForm: function() {
        for (var t in this.data.orders_goods) if (!this.data.orders_goods[t].diyform_data_id) return !1;
        return !0;
    },
    _setDiyForm: function(t, e) {
        for (var i in this.data.orders_goods) if (this.data.orders_goods[i].pre_id == t) return this.data.orders_goods[i].diyform_data_id = e, 
        void this.setData({
            orders_goods: this.data.orders_goods,
            "diyform.diyform_data_id": e
        });
    },
    _findDiyForm: function(t) {
        for (var e in this.data.orders_goods) if (this.data.orders_goods[e].pre_id == t) return this.data.orders_goods[e].diyform_data_id;
        return !1;
    },
    _assembleDiyForm: function() {
        var t = [];
        for (var e in this.data.orders_goods) t.push(this.data.orders_goods[e]);
        return t;
    },
    getOrerData: function() {
        var t = this, i = e.getNetAddresss(t.data.ORDER_URL);
        i += "&enter_at=" + this.data.start, i += "&leave_at=" + this.data.end, i += "&hotel_id=" + this.data.id, 
        i += "&goods_id=" + this.data.goods_id, i += "&goods_option_id=", i += "&total=" + this.data.total, 
        i += "&dispatch_type_id=4", i += "&member_coupon_ids=" + this.assembleCoupons(), 
        i += "&orders=" + JSON.stringify(this._assembleDeduction()), e._getNetWork({
            url: i,
            success: function(e) {
                var i = e.data;
                1 == i.result ? (t.setData({
                    order_info: i.data,
                    title: i.data.orders[0].order_goods[0].title
                }), t._initDiyForm(), t.findGoodOpen(), t.getTime(i.data.orders[0].hotel_order), 
                t.initCoupon(i.data.discount), t.setData({
                    cup_notice: !0
                })) : t.setData({
                    btn_applyShow: !0,
                    cup_notice: !0
                });
            },
            fail: function(t) {
                console.log(t.msg);
            }
        });
    },
    getTime: function(t) {
        var e = t.enter_at.split(" "), i = t.leave_at.split(" "), o = e[0].split("-"), a = i[0].split("-");
        this.setData({
            start_time: "".concat(o[1], "月").concat(o[2], "日"),
            end_time: "".concat(a[1], "月").concat(a[2], "日")
        }), this.getDays(e[0], i[0]);
    },
    getDays: function(t, e) {
        var i, o, a;
        i = t.split("-"), o = e.split("-");
        var s = new Date(i[0], i[1] - 1, i[2]), d = new Date(o[0], o[1] - 1, o[2]);
        a = parseInt(Math.abs(s - d) / 1e3 / 60 / 60 / 24), this.setData({
            night_day: a
        });
    },
    getCustomizeHotelHead: function() {
        var t = this, i = "";
        try {
            var o = wx.getStorageSync("customizeHotelHead");
            o && (i = o);
        } catch (t) {}
        if (e._isTextEmpty(i)) {
            var a = e.getNetAddresss("plugin.hotel.frontend.hotel.get-hotel-info.get-custom-name");
            e._getNetWork({
                url: a,
                success: function(e) {
                    var i = e.data;
                    if (1 == i.result) {
                        t.setData({
                            PageNameList: i.data
                        }), wx.setNavigationBarTitle({
                            title: i.data.goods_details ? i.data.goods_details : ""
                        });
                        try {
                            wx.setStorageSync("customizeHotelHead", JSON.stringify(i.data));
                        } catch (t) {
                            console.log(t);
                        }
                    }
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        } else this.setData({
            PageNameList: JSON.parse(i)
        }), wx.setNavigationBarTitle({
            title: this.data.PageNameList.goods_details
        });
    },
    invoiceChange: function(t) {
        var e = t.detail;
        this.setData({
            isInvoice: e
        });
    },
    addTotal: function(t) {
        var e = t.detail;
        this.setData({
            total: e
        }), this.getOrerData();
    },
    nameinp: function(t) {
        var e = t.detail.value;
        this.setData({
            name: e
        });
    },
    mobilinp: function(t) {
        var e = t.detail.value;
        this.setData({
            mobil: e
        });
    },
    eInvoiceChange: function(t) {
        var e = t.detail;
        this.setData({
            el_invoice: e
        });
    },
    typeChange: function(t) {
        var e = t.detail;
        this.setData({
            invoice_type: e
        });
    },
    fp_ttinp: function(t) {
        var e = t.detail.value;
        this.setData({
            fp_tt: e
        });
    },
    company_nameinp: function(t) {
        var e = t.detail.value;
        this.setData({
            company_name: e
        });
    },
    sbxdinp: function(t) {
        var e = t.detail.value;
        this.setData({
            sbxd: e
        });
    },
    submit: function() {
        if (e._isTextEmpty(this.data.name)) wx.showToast({
            icon: "none",
            title: "请填写入住人",
            duration: 1500
        }); else if (e._isTextEmpty(this.data.mobil)) wx.showToast({
            icon: "none",
            title: "请填写电话号码",
            duration: 1500
        }); else {
            if (this.data.isInvoice) {
                if ("1" == this.data.invoice_type && e._isTextEmpty(this.data.fp_tt)) return void wx.showToast({
                    icon: "none",
                    title: "请输入发票抬头",
                    duration: 1500
                });
                if ("2" == this.data.invoice_type) {
                    if (e._isTextEmpty(this.data.company_name)) return void wx.showToast({
                        icon: "none",
                        title: "请填写单位名称",
                        duration: 1500
                    });
                    if (e._isTextEmpty(this.data.sbxd)) return void wx.showToast({
                        icon: "none",
                        title: "请填写纳税人识别向导",
                        duration: 1500
                    });
                }
            }
            if (!this.data.hasGoodOpenDF || this._checkDiyForm()) {
                var t = e.getNetAddresss(this.data.SUBMIT_URL);
                t += "&enter_at=" + this.data.start, t += "&leave_at=" + this.data.end, t += "&hotel_id=" + this.data.id, 
                t += "&dispatch_type_id=4", t += "&member_coupon_ids=" + JSON.stringify(this.assembleCoupons()), 
                t += "&goods=" + JSON.stringify([ {
                    goods_id: this.data.goods_id,
                    goods_option_id: "",
                    total: this.data.total
                } ]), t += "&address=" + JSON.stringify({}), t += "&orders=" + JSON.stringify(this._assembleDeduction()), 
                t += "&mobile=" + this.data.mobil, t += "&realname=" + this.data.name, t += "&invoice_type=" + Number(1 == this.data.el_invoice ? "0" : "1"), 
                t += "&rise_type=" + Number(1 == this.data.invoice_type ? "1" : "0"), t += "&call=" + ("1" == this.data.invoice_type ? this.data.fp_tt : this.data.company_name), 
                t += "&company_number=" + this.data.sbxd, this.data.hasGoodOpenDF && (t += "&order_goods=" + JSON.stringify(this._assembleDiyForm())), 
                e._getNetWork({
                    url: t,
                    success: function(t) {
                        var e = t.data;
                        1 == e.result ? wx.navigateTo({
                            url: "/packageD/buy/orderPay/orderPay?status=1&order_id=" + e.data.order_ids
                        }) : wx.showToast({
                            icon: "none",
                            title: e.msg,
                            duration: 1500
                        });
                    },
                    fail: function(t) {
                        console.log(t);
                    }
                });
            } else wx.showToast({
                icon: "none",
                title: "请先填写表单",
                duration: 1500
            });
        }
    },
    initCoupon: function(t) {
        this.setData({
            isShowCoupon: "1" == t.whether_show_coupon
        });
        var e = t.member_coupons;
        null != e && "" != e && null != e && this.setData({
            coupon_size: e.length
        }), this.setData({
            coupons: e
        });
    },
    showCoupon: function() {
        0 != this.data.coupon_size ? this.setData({
            popupCouponSpecs: !0
        }) : wx.showToast({
            icon: "none",
            title: "暂无优惠券使用",
            duration: 1500
        });
    },
    couponclose: function() {
        this.setData({
            popupCouponSpecs: !1
        });
    },
    selectCoupon: function(e) {
        var i = e.detail, o = e.currentTarget.dataset.index, a = e.currentTarget.dataset.item;
        this.setData(t({}, "coupons[" + o + "].checked", i)), this.screenCoupon(i, a), this.setData({
            use_coupon_size: this.data.checkCouponList.length
        }), this.getOrerData();
    },
    screenCoupon: function(t, e) {
        var i = e;
        if (t) if (this.setData({
            cup_notice: !1
        }), this.data.checkCouponList.length > 0) {
            for (var o = 0; o < this.data.checkCouponList.length; o++) if (this.data.checkCouponList[o].id == i.id) {
                var a = this.data.checkCouponList;
                a.splice(o, 1), this.setData({
                    checkCouponList: a
                });
            }
            var s = this.data.checkCouponList;
            s.push(i), this.setData({
                checkCouponList: s
            });
        } else {
            var d = this.data.checkCouponList;
            d.push(i), this.setData({
                checkCouponList: d
            });
        } else if (this.setData({
            cup_notice: !0
        }), this.data.checkCouponList.length > 0) for (var n = 0; n < this.data.checkCouponList.length; n++) if (this.data.checkCouponList[n].id == i.id) {
            var r = this.data.checkCouponList;
            r.splice(n, 1), this.setData({
                checkCouponList: r
            });
        }
    },
    assembleCoupons: function() {
        for (var t = [], e = 0; e < this.data.checkCouponList.length; e++) t.push(this.data.checkCouponList[e].id);
        return t;
    },
    discountHandle: function(e) {
        var i = e.detail, o = e.currentTarget.dataset.dindex;
        "[object Object]" == Object.prototype.toString.call(this.data.order_info.orders[0].order_deductions) ? this.setData(t({}, "order_info.orders[0].order_deductions." + o + ".checked", i)) : "[object Array]" == Object.prototype.toString.call(this.data.order_info.orders[0].order_deductions) && this.setData(t({}, "order_info.orders[0].order_deductions[" + o + "]checked", i)), 
        this._screenDiscount(this.data.order_info.orders[0], this.data.order_info.orders[0].order_deductions[o]), 
        this.getOrerData();
    },
    _screenDiscount: function(t, i) {
        var o = {}, a = [], s = [];
        if (i.checked) if (this.data.checkDeductionList.length > 0) {
            for (var d = 0; d < this.data.checkDeductionList.length; d++) if (this.data.checkDeductionList[d].pre_id == t.pre_id) {
                a = e._isTextEmpty(this.data.checkDeductionList[d].deduction_ids) ? [] : this.data.checkDeductionList[d].deduction_ids, 
                (s = this.data.checkDeductionList).splice(d, 1), this.setData({
                    checkDeductionList: s
                });
                break;
            }
            if (a.length > 0) {
                for (var n = 0; n < a.length; n++) a[n] == i.code && a.splice(n, 1);
                a.push(i.code), o.deduction_ids = a, o.pre_id = t.pre_id, s.push(o), this.setData({
                    checkDeductionList: s
                });
            } else a.push(i.code), o.deduction_ids = a, o.pre_id = t.pre_id, s.push(o), this.setData({
                checkDeductionList: s
            });
        } else if (a.length > 0) {
            for (var r = 0; r < a.length; r++) a[r] == i.code && a.splice(r, 1);
            a.push(i.code), o.deduction_ids = a, o.pre_id = t.pre_id, s.push(o), this.setData({
                checkDeductionList: s
            });
        } else a.push(i.code), o.deduction_ids = a, o.pre_id = t.pre_id, s.push(o), this.setData({
            checkDeductionList: s
        }); else if (this.data.checkDeductionList.length > 0) {
            for (var c = 0; c < this.data.checkDeductionList.length; c++) if (this.data.checkDeductionList[c].pre_id == t.pre_id) {
                a = e._isTextEmpty(this.data.checkDeductionList[c].deduction_ids) ? [] : this.data.checkDeductionList[c].deduction_ids;
                var h = this.data.checkDeductionList;
                h.splice(c, 1), this.setData({
                    checkDeductionList: h
                });
                break;
            }
            if (a.length > 0) {
                for (var u = 0; u < a.length; u++) a[u] == i.code && a.splice(u, 1);
                o.deduction_ids = a, o.pre_id = t.pre_id, s.push(o), this.setData({
                    checkDeductionList: s
                });
            } else o.deduction_ids = a, o.pre_id = t.pre_id, s.push(o), this.setData({
                checkDeductionList: s
            });
        } else if (a.length > 0) {
            for (var _ = 0; _ < a.length; _++) a[_] == i.code && a.splice(_, 1);
            o.deduction_ids = a, o.pre_id = t.pre_id, s.push(o), this.setData({
                checkDeductionList: s
            });
        } else o.deduction_ids = a, o.pre_id = t.pre_id, s.push(o), this.setData({
            checkDeductionList: s
        });
    },
    _assembleDeduction: function() {
        for (var t = [], e = 0; e < this.data.checkDeductionList.length; e++) t.push(this.data.checkDeductionList[e]);
        return t;
    }
});